package com.teckteam.ptvsystem.model;

import java.util.ArrayList;
import java.util.List;

import twitter4j.Paging;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

import com.teckteam.ptvsystem.controller.ScrapeFailedException;

/**
 * Twitter feed helper class to provide up-to-date information from official Twitter channels
 * Requires internet connection.
 * 
 * Idea adapted from Group SEPT30's implementation
 */
public class TwitterHelper
{
   private static ArrayList<String>
      metroFeed = new ArrayList<String>(),
      yarraFeed = new ArrayList<String>(),
      metlinkFeed = new ArrayList<String>();
   
   public static void scrapeTwitterFeed()
   {
      //instance of the twitter class
      Twitter t = new TwitterFactory().getInstance();
      
      metroFeed.removeAll(metroFeed);
      yarraFeed.removeAll(yarraFeed);
      metlinkFeed.removeAll(metlinkFeed);
      
      try {
         /*(i, j) 
          * where i is the number of pages and 
          * j is the number of statuses 
          */
         Paging paging = new Paging(1, 3);
         /*
          * Stores the specified user's
          * timeline in a List of stauses
          * with the paging defined above 
          */
         List<Status> yarra = t.getUserTimeline("yarratrams", paging);
         List<Status> metlink = t.getUserTimeline("MetlinkUpdates", paging);
         List<Status> metro = t.getUserTimeline("metrotrains", paging);
         
         /*
          * Iterate through the lists 
          * printing the statuses 
          * that have been stored within them
          */
         yarraFeed.add(yarra.get(0).getUser().getName());
         for (Status status : yarra) 
         {
            String
               name = yarraFeed.get(0),
               msg = status.getText();
               
            System.out.println(name + " said " + msg);
            yarraFeed.add(msg);
         }
         System.out.println();
         
         metlinkFeed.add(metlink.get(0).getUser().getName());
         for (Status status : metlink)
         {
            String
               name = metlinkFeed.get(0),
               msg = status.getText();
               
            System.out.println(name + " said " + msg);
            metlinkFeed.add(msg);
         }
         System.out.println();
         
         metroFeed.add(metro.get(0).getUser().getName());
         for (Status status : metro)
         {
            String
               name = metroFeed.get(0),
               msg = status.getText();
            
            System.out.println(name + " said " + msg);
            metroFeed.add(msg);
         }
      } catch (TwitterException e) {
         //e.printStackTrace();
         System.err.println("150 requests per hour limit reached, or bad connection.");
      }
   }
   
   /**
    * Gets Metro Twitter Feed using internet; three latest entries.
    * @return
    * @throws ScrapeFailedException
    */
   public static ArrayList<String> getMetroFeed()
   {
      if(metroFeed.isEmpty()) scrapeTwitterFeed();
      return metroFeed;
   }
   
   /**
    * Gets Metlink Twitter Feed using internet; three latest entries.
    * @return
    * @throws ScrapeFailedException
    */
   public static ArrayList<String> getMetlinkFeed()
   {
      if(metlinkFeed.isEmpty()) scrapeTwitterFeed();
      return metlinkFeed;
   }
   
   /**
    * Gets Yarra Trams Twitter Feed using internet; three latest entries.
    * @return
    * @throws ScrapeFailedException
    */
   public static ArrayList<String> getYarraFeed()
   {
      if(yarraFeed.isEmpty()) scrapeTwitterFeed();
      return yarraFeed;
   }
}
